<?php

namespace app\common\model;

use app\common\service\StatusService;
use think\Model;


class Order extends Model
{
	
	
	// 表名
	protected $name = 'order';
	
	// 自动写入时间戳字段
	protected $autoWriteTimestamp = true;
	
	// 定义时间戳字段名
	protected $deleteTime = false;
	
	// 追加属性
	protected $append = [
		'pay_time_text',
		'shipping_time_text',
		'sign_time_text',
		'create_time_text',
		'update_time_text',
		'order_type_text',
		'status_text',
		'pay_status_text',
		'pay_type_text',
		'shipping_status_text',
		'close_type_text',
		'comment_status_text'
	];
	
	
	public function getOrderTypeTextAttr($value, $data)
	{
		$value = $value ?: $data['order_type'];
		$list = StatusService::$orderTypeList;
		return $list[$value] ?? '';
	}
	
	
	public function getStatusTextAttr($value, $data)
	{
		$value = $value ?: $data['status'];
		$list = StatusService::$orderStatusList;
		return $list[$value] ?? '';
	}
	
	public function getPayStatusTextAttr($value, $data)
	{
		$value = $value ?: $data['pay_status'];
		$list = StatusService::$orderPaymentStatusList;
		return $list[$value] ?? '';
	}
	
	public function getPayTypeTextAttr($value, $data)
	{
		$value = $value ?: $data['pay_type'];
		$list = StatusService::$payTypeList;
		return $list[$value] ?? '';
	}
	
	public function getShippingStatusTextAttr($value, $data)
	{
		$value = $value ?: $data['shipping_status'];
		$list = StatusService::$orderShippingList;
		return $list[$value] ?? '';
	}
	
	public function getCloseTypeTextAttr($value, $data)
	{
		$value = $value ?: $data['close_type'];
		$list = StatusService::$orderCloseType;
		return $list[$value] ?? '';
	}
	
	public function getCommentStatusTextAttr($value, $data)
	{
		$value = $value ?: $data['comment_status'];
		$list = StatusService::$orderCommentStatusList;
		return $list[$value] ?? '';
	}
	
	public function user()
	{
		return $this->belongsTo('user', 'user_id', 'id', '', 'left')->setEagerlyType(0);
	}
	
	public function recommend()
	{
		return $this->belongsTo('user', 'recommend_user_id', 'id', '', 'left')->setEagerlyType(0);
	}
	
	public function seller()
	{
		return $this->belongsTo('seller', 'seller_id', 'id', '', 'left')->setEagerlyType(0);
	}
	
	public function getPayTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['pay_time']) ? $data['pay_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getShippingTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['shipping_time']) ? $data['shipping_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getSignTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['sign_time']) ? $data['sign_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getUpdateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }
	
	/**
	 * 添加订单
	 * @param $userId //用户
	 * @param $sellerId //商家
	 * @param $amount //订单金额
	 * @param $goodsAmount //商品金额
	 * @param $number //数量
	 * @param string $recommendUserId 推荐人
	 * @param string $remark 备注
	 * @param int $orderType 订单类型
	 * @param int $status 状态
	 * @param string $activityId 活动ID
	 * @param string $recordId
	 * @param int $depositAmount 定金
	 * @return bool|string
	 */
	public function addOrder($userId, $sellerId, $amount, $goodsAmount, $number, $recommendUserId = '', $remark = '', $orderType = StatusService::ORDER_TYPE_NORMAL, $status = StatusService::ORDER_WAIT_PAY, $activityId = '', $recordId = '', $depositAmount = 0) {
		$res = $this->isUpdate(false)->save([
			'user_id' => $userId,
			'seller_id' => $sellerId,
			'order_no' => time().$userId.mt_rand(1000, 9999),
			'order_type' => $orderType,
			'amount' => $amount,
			'goods_amount' => $goodsAmount,
			'number' => $number,
			'status' => $status,
			'remark' => $remark,
			'activity_id' => $activityId,
			'record_id' => $recordId,
			'recommend_user_id' => $recommendUserId,
			'deposit_amount' => $depositAmount
		]);
		
		if($res) {
			return $this->getLastInsID();
		} else {
			return false;
		}
	}
	
	/**
	 * 根据ID获取订单
	 * @param $id
	 * @return array|false|\PDOStatement|string|Model
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 */
	public function getOrderById($id)
	{
		return $this->find($id);
	}
	
	public function changeOrderData($orderId, array $data)
	{
		if(empty($orderId) || empty($data)) {
			$this->error = '参数错误';
			return false;
		}
		
		$res = $this->save($data, ['id' => $orderId]);
		return $res;
	}
	
	
	protected function setPayTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setShippingTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setSignTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setCreateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setUpdateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


}
